package org.bigjava.buff.user.service.impl;

import org.bigjava.buff.common.util.DealNumberUtil;
import org.bigjava.buff.common.util.Page;
import org.bigjava.buff.goods.bean.ExteriorAndPrice;
import org.bigjava.buff.goods.bean.Type;
import org.bigjava.buff.goods.mapper.GoodsMapper;
import org.bigjava.buff.goods.mapper.TypeMapper;
import org.bigjava.buff.goods.service.TypeService;
import org.bigjava.buff.user.bean.Deal;
import org.bigjava.buff.user.bean.User;
import org.bigjava.buff.user.mapper.DealMapper;
import org.bigjava.buff.user.service.DealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DealServiceImpl implements DealService {

    @Autowired
    private DealMapper dealMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private TypeService typeService;

    public Deal createDeal(ExteriorAndPrice ep, User user, Integer dealType, Double price) {

        //交易对象
        Deal deal = new Deal();

        //生成交易号
        String orderNum = DealNumberUtil.getLocalTrmSeqNum();
        deal.setOrderNumber(orderNum);
        //交易的物品
        deal.setEp(ep);
        //交易用户
        deal.setUser(user);
        //交易类型 1/出售 2/求购
        deal.setDealType(dealType);
        //交易状态  1/在售或者求购 2/以售出但未发货 3/已发货
        deal.setState(1);
        //发起的交易金额
        deal.setPrice(price);
        //交易发起时间
        deal.setDealTime(new Date());
        //交易最终结果
        deal.setResult(0);

        dealMapper.createDeal(deal);

        return deal;
    }

    /**
     * 查询用户交易信息
     * @param userId    用户id
     * @param dealType  1/出售 2/求购
     * @return  交易物品结果集
     */
    public List getGoodsInSell(Integer userId,Integer dealType) {

        Map map = new HashMap();
        map.put("userId", userId);
        map.put("dealType", dealType);

        List<Deal> sellDealList = dealMapper.selectDeal(map);
        for(Deal d : sellDealList){
            d.getEp().getGoods().setGoodsType(typeMapper.getTypeById(d.getEp().getGoods().getGoodsType().getId()));
        }

        return sellDealList;
    }

    /**
     * 获取物品交易信息
     * @param goodsId   交易物品id
     * @param dealType  交易类型
     * @return  交易信息集合
     */
    public List getGoodsDealByGoodsId(Integer goodsId, Integer dealType) {
        Map map = new HashMap();

        List<ExteriorAndPrice> list = dealMapper.getExterByGoodsId(goodsId);
        List dealList = new ArrayList();
        for(ExteriorAndPrice e : list){
            System.out.println(e.getId());
            map.put("eId", e.getId());
            map.put("dealType", dealType);
            Deal deal = dealMapper.getGoodsDeal(map);
            if(deal != null) {
                String tellphone = deal.getUser().getTellphone();
                deal.getUser().setTellphone(tellphone.substring(0,3) + "****" + tellphone.substring(7));
                dealList.add(deal);
            }
        }

        return dealList;
    }

    /**
     * 批量上架
     * @param deals 封装好的交易对象集合
     * @return 用户出售交易集合
     */
    public List<Deal> batchDeal(List<Deal> deals) {

        for(Deal deal :deals){
            this.createDeal(deal.getEp(), deal.getUser(), deal.getDealType(), deal.getPrice());
        }
        Integer uId = deals.get(0).getUser().getId();
        List sellList = this.getGoodsInSell(uId,1);
        return sellList;
    }

    /**
     * 获取最新交易集合
     * @param num 获取的条数
     * @return  交易集合
     */
    public List<Deal> getNewDealList(Integer num) {

        return null;
    }

    /**
     * 批量下架交易饰品
     * @param ids   交易id集合
     */
    public void batchSoldOutGoods(Integer[] ids) {

        for(Integer id : ids){
            dealMapper.soldOutGoods(id);
        }
    }

    /**
     * 通过订单号查询订单
     * @param orderNum  订单号
     * @return 订单详情
     */
    public Deal getDealByOrderNum(String orderNum) {
        return dealMapper.getDealByOrderNum(orderNum);
    }

    /**
     * 关闭交易
     * @param   did 交易id
      * @return 影响行数
     */
    public int closeDeal(Integer did) {
        return dealMapper.closeDeal(did);
    }


    /**
     * 获取交易中的物品
     * @param dealType  交易类型 1/2 出售/求购
     * @param page  页面信息 每页显示条数和当前页
     * @return  页面显示结果集
     */
    public Page getDeal(Integer dealType, Page page){

        //计算总页数
        page.setTotal( dealMapper.getDealTotal(dealType));

        //如果当前页大于最大页数 当前重置为第一页
        if(page.getPageNum() > page.getPages()) page.setPageNum(1);

        Map map = new HashMap();
        map.put("dealType", dealType);
        map.put("pageNum", (page.getPageNum() - 1) * page.getPageSize());
        map.put("pageSize", page.getPageSize());

        List<Deal> list = dealMapper.getDeal(map);
        List<Type> allTypeList = typeMapper.findtypeByfid(0);
        for (Deal deal : list) {
            ExteriorAndPrice e = deal.getEp();
            e.setGoods(typeService.fillGoodsType(e.getGoods()));
        }
        page.setList(list);

        return page;
    }

    /**
     * 获取10条最新交易信息
     * @return  交易集合
     */
    public List getNewDeal() {
        List<Deal> list = dealMapper.getNewDeal();
        for(Deal deal : list){
            ExteriorAndPrice ep = deal.getEp();
            ep.setGoods(typeService.fillGoodsType(ep.getGoods()));
        }
        return list;
    }



}
